// 根目录的static下创建js文件夹并创建mixin.js文件
export const mixin = {
    data() {
        return {
            keyClass: {
                unpress: 'piano-key wkey unpress', press: 'piano-key wkey press'

            }, stateMap: [0, 0, 0, 0, 0, 0, 0],

            websocket: null,
        }
    }, methods: {
        printLog(line) {
            let dom = document.getElementById("console")
            dom.innerHTML = dom.innerHTML + line + "<br>"
            dom.scrollTop = dom.scrollHeight;
        }, formatLog(line) {
            return `${new Date().format('yyyy-mm-dd hh:MM:ss')}     ${line}`
        }, //按压键盘
        pressKey(keyCode) {
            console.log('按下', keyCode)
            let key = document.getElementById(`piano-key-${keyCode}`)
            console.log(key)
            this.printLog(this.formatLog('按下' + keyCode))
            this.playAudio(keyCode)
            key.setAttribute("class", this.keyClass.press);
        },

        unpressKey(keyCode) {
            console.log('弹起', keyCode)
            this.printLog(this.formatLog('弹起' + keyCode))
            let key = document.getElementById(`piano-key-${keyCode}`)
            console.log(key)
            key.setAttribute("class", this.keyClass.unpress);
        }, playAudio(keyCode) {
            let mp3Url = `../static/audio/${keyCode}.m4a`;
            let player = new Audio(mp3Url);
            player.play(); //播放 mp3这个音频对象
        },


    }
}
